package com.example.test_01.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.test_01.R;
import com.example.test_01.adapter.FoodAdapter;
import com.example.test_01.adapter.FoodTypeAdapter;
import com.example.test_01.model.Food;
import com.example.test_01.model.FoodType;
import com.example.test_01.service.FoodService;
import com.example.test_01.service.FoodServiceImpl;
import com.example.test_01.service.FoodTypeService;
import com.example.test_01.service.FoodTypeServiceImpl;
import com.example.test_01.utils.Result;

import java.util.List;

public class FoodList extends BaseActivity  implements View.OnClickListener{
    private static final String TAG = "FoodList";
    private final FoodService service = new FoodServiceImpl();

    //    配置spinner
    private final FoodTypeService typeService = new FoodTypeServiceImpl();
    private List<String> typeNames;
    private ArrayAdapter<String> typeNameAdapter;

    AlertDialog.Builder builder;
    Button btn_add_food;
    FoodAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);

        btn_add_food = findViewById(R.id.btn_add_food);
        btn_add_food.setOnClickListener(this);

        RecyclerView recyclerView = findViewById(R.id.rv_food_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new FoodAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add_food:
                View view2 = View.inflate(this,R.layout.food_form,null);
                // 配置下拉框
                typeNames = typeService.GetAllName();
                typeNameAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,typeNames);

                final EditText input_food_name = view2.findViewById(R.id.input_food_name);
                final EditText input_food_price = view2.findViewById(R.id.input_food_price);
                final Spinner spinner_food_type = view2.findViewById(R.id.spinner_food_type);

                spinner_food_type.setAdapter(typeNameAdapter);

                builder = new AlertDialog.Builder(this);
                builder.setTitle("菜品分类").setView(view2);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Food food = new Food();
                        String name = input_food_name.getText().toString();
                        String price = input_food_price.getText().toString();
                        String type = spinner_food_type.getSelectedItem().toString();
                        food.setName(name);
                        food.setPrice(Double.valueOf(price));
                        food.setFoodType(type);
                        Result rec = service.Add(food);
                        if(rec == Result.SUCCESS){
                            Toast.makeText(FoodList.this,"添加成功",Toast.LENGTH_SHORT).show();
                            adapter.reload();
                            dialogInterface.dismiss();
                        }
                    }
                });
                builder.create().show();
                break;
        }
    }
}